# -*- coding: utf-8 -*-
"""
@Time ： 2023/4/1 20:17
@Auth ： Mr. William 1052949192
@Company ：特斯汀学院 @testingedu.com.cn
@Function ：主流程最大化
"""
import time

from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.chrome.options import Options

# 登录，修改个人信息，新增-删除收货地址，搜索，加购物车，下单，获取订单，查询
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

option = Options()
# 不自动关闭浏览器的配置
option.add_experimental_option("detach", True)
driver = webdriver.Chrome(options=option)
driver.maximize_window()
driver.implicitly_wait(3)

# 登录
driver.get('http://testingedu.com.cn:8000/Home/user/login.html')
# 定位元素-操作元素
driver.find_element('xpath', '//*[@id="username"]').send_keys('13800138006')
driver.find_element('xpath', '//*[@id="password"]').send_keys('123456')
driver.find_element('xpath', '//*[@id="verify_code"]').send_keys('1111')
driver.find_element('xpath', '//*[@id="loginform"]/div/div[6]/a').click()

# # element click intercepted（元素点击被拦截）
# time.sleep(1)
# # 把挡住的元素关闭
# driver.find_element('xpath','//a[text()="确定"]').click()
# driver.find_element('xpath', '//*[@id="loginform"]/div/div[6]/a').click()
# 获取昵称
nick = driver.find_element('xpath', '//a[@class="red userinfo"]').text
print(nick)

# 修改个人信息
# # 获得find_element找到的元素对象
# ele = driver.find_element('xpath','//span[text()="账户设置"]')
# # ActionChains是一个鼠标操作的类
# action = ActionChains(driver)
# # .perform()，让前面的一系列鼠标操作生效
# action.move_to_element(ele).perform()

# # 我就要点第二个：这里的下标从1开始
# driver.find_element('xpath', '(//a[contains(text(),"个人")])[2]').click()

# # 使用URL地址进入，可以消除页面位置的依赖
# driver.get('http://testingedu.com.cn:8000/Home/User/info.html')
# driver.find_element('xpath', '//*[@id="preview"]').click()
#
# # 找到iframe
# iframe = driver.find_element('xpath', '//iframe')
# # 进入iframe
# driver.switch_to.frame(iframe)
# # 图片上传
# driver.find_element('xpath', '//*[@id="filePicker"]//input') \
#     .send_keys(r'C:\Users\Amy\Desktop\will.png')
#
# # # iframe切出
# # driver.switch_to.default_content()
# # driver.find_element('xpath','//a[contains(@class,"layui-layer-close1")]').click()
#
# # ele = driver.find_element('xpath', '//div[@class="progress"]/span')
# # # 如果text获取不到文本，可以获取innerText属性
# # print(ele.text)
# # element_text = ele.get_attribute('innerText')
# # print(element_text)
# # print('100%' in element_text)
#
# # 等进度的文本变成100%再点确定
# WebDriverWait(driver, 10, 0.5).until(
#     EC.text_to_be_present_in_element_attribute(
#         ('xpath', '//div[@class="progress"]/span'),'innerText', '100%'))
#
# driver.find_element('xpath', '//div[text()="确定使用"]').click()

# # 新增-删除收货地址
# driver.get('http://testingedu.com.cn:8000/Home/User/address_list.html')
# driver.find_element('xpath','//*[text()="增加新地址"]').click()
#
# # 输入信息
# driver.find_element('xpath','//input[@name="consignee"]').send_keys('老Will')
# driver.find_element('xpath','//input[@name="mobile"]').send_keys('18888888887')
# # 找到下拉框
# ele = driver.find_element('xpath','//*[@id="province"]')
# province = Select(ele)
# province.select_by_index(18)
#
# # 找到下拉框
# ele = driver.find_element('xpath','//*[@id="city"]')
# city = Select(ele)
# city.select_by_value('25580')
#
# # 找到下拉框
# ele = driver.find_element('xpath','//*[@id="district"]')
# district = Select(ele)
# district.select_by_visible_text('岳麓区')
#
#
# driver.find_element('xpath','//input[@name="address"]').send_keys('老Will的测试地址')
# driver.find_element('xpath','//a[@id="address_submit"]').click()
#
# time.sleep(2)
# # 一定是删除我最近添加的一个叫老Will的地址
# driver.find_element('xpath','//span[text()="老Will"]/../..//a[text()="删除"]').click()

# 搜索，加购物车，下单，获取订单，查询
driver.find_element('xpath','//*[@id="q"]').send_keys('手机')
driver.find_element('xpath','//*[@id="sourch_form"]/a').click()
driver.find_element('xpath','//a[contains(text(),"苹果7钢")]/../..//a[text()="加入购物车"]').click()
# 固定
time.sleep(1)
driver.find_element('xpath','//*[@class="layui-layer-setwin"]/a').click()
driver.find_element('xpath','//*[text()="我的购物车"]').click()
driver.find_element('xpath','//*[text()="去结算"]').click()
driver.find_element('xpath','//*[@class="checkout-submit"]').click()

# 获取订单，查询
# 获取文本
order_text = driver.find_element('xpath','//p[@class="succ-p"]').text
print(order_text)

# 正则提取订单号：18位的连续数字
import re
order_id = re.findall(r'\d{18}',order_text)[0]
print(order_id)

# 进入订单页面，查询订单
driver.find_element('xpath','//a[text()="我的订单"]').click()
handles = driver.window_handles

# # 按下标
# driver.switch_to.window(handles[-1])
# driver.find_element('xpath','//*[@id="search_key"]').send_keys('order_id')


# 按title切换
for h in handles:
    driver.switch_to.window(h)
    title = driver.title
    if title == '我的订单':
        break

driver.find_element('xpath','//*[@id="search_key"]').send_keys(order_id)

driver.find_element('xpath','//a[text()="取消订单"]').click()
driver.find_element('xpath','//a[text()="确定"]').click()
driver.find_element('xpath','//a[text()="确定"]').click()

